package com.lh.controller.center;

import com.lh.controller.BaseController;
import com.lh.enums.YesOrNo;
import com.lh.pojo.OrderItems;
import com.lh.pojo.Orders;
import com.lh.pojo.bo.center.OrderItemsCommentBO;
import com.lh.service.center.MyCommentsService;
import com.lh.utils.IMOOCJSONResult;
import com.lh.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: foodie-dev
 * @description:
 * @author: LH
 * @create: 2022-05-30 09:59
 **/
@Api(value = "我的评价相关接口", tags = {"我的评价相关api接口"})
@RestController
@RequestMapping("/mycomments")
public class MyCommentsController extends BaseController {

    @Autowired
    private MyCommentsService myCommentsService;

    @ApiOperation(value = "可评价商品获取", notes = "可评价商品获取", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/pending")
    public IMOOCJSONResult pending(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId
    ){

        //判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        //判断该订单是否已经评价过
        Orders myOrder = (Orders) checkResult.getData();
        if (myOrder.getIsComment().equals(YesOrNo.YES.getType())){
            return IMOOCJSONResult.errorMsg("该笔订单已经评价");
        }

        List<OrderItems> itemsList = myCommentsService.pending(orderId);

        return IMOOCJSONResult.ok(itemsList);
    }

    @ApiOperation(value = "保存商品评价", notes = "保存商品评价", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/saveList")
    public IMOOCJSONResult saveList(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList
    ){
        // 判断用户和订单是否关联
        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }
        // 判断评论内容list不能为空
        if (commentList == null || commentList.isEmpty() || commentList.size() == 0) {
            return IMOOCJSONResult.errorMsg("评论内容不能为空！");
        }

        myCommentsService.saveList(userId,orderId,commentList);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "我的所有评价获取", notes = "我的所有评价获取", httpMethod = "POST",
            response = IMOOCJSONResult.class,
            produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/query")
    public IMOOCJSONResult query(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "页数", value = "页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "每页条数", value = "每页条数", required = false)
            @RequestParam Integer pageSize
    ){

        if (page == null){
            page = 1;
        }
        if (pageSize == null){
            pageSize = PAGE_SIZE;
        }

        PagedGridResult result = myCommentsService.query(userId, page, pageSize);

        return IMOOCJSONResult.ok(result);
    }

}
